package com.fh.shop.api.book.controller;

import com.fh.shop.api.book.biz.IBookService;
import com.fh.shop.api.book.param.BookQueryParam;
import com.fh.shop.api.book.po.Book;
import com.fh.shop.api.common.DataTableReponse;
import com.fh.shop.api.common.ServerResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@CrossOrigin
@RequestMapping("/api/books")
@Api(tags = "图书接口类", description = "图书restful接口")
public class BookRestController {
    @Resource(name = "bookService")
    private IBookService bookService;

//    @RequestMapping(method = RequestMethod.POST)
    @PostMapping
    @ApiOperation("添加图书")
    public ServerResponse add(Book book) {
        return bookService.addBook(book);
    }

//    @RequestMapping(method = RequestMethod.GET)
    @GetMapping
    @ApiOperation("查询图书的分页列表")
    public DataTableReponse findPageList(BookQueryParam bookQueryParam) {
        return bookService.findPageList(bookQueryParam);
    }

//    @RequestMapping(value = "/api/books/{xx}/tt/fh/{age}", method = RequestMethod.DELETE)
//    public ServerResponse delete(@PathVariable("xx") Long id, @PathVariable Integer age) {
//        return bookService.deleteBook(id);
//    }

//    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @DeleteMapping("/{id}")
    @ApiOperation("根据id删除图书")
    @ApiImplicitParam(name = "id", value = "图书id", dataType = "java.lang.Long", required = true, example = "0")
    public ServerResponse delete(@PathVariable Long id) {
        return bookService.deleteBook(id);
    }

//    @RequestMapping(method = RequestMethod.DELETE)
    @DeleteMapping
    @ApiOperation("批量删除图书")
    @ApiImplicitParam(name = "ids", value = "多个图书id,用逗号隔开,如:1,2,3", dataType = "java.lang.String", required = true)
    public ServerResponse deleteBatch(String ids) {
        return bookService.deleteBatch(ids);
    }

//    @RequestMapping(value = "/{bookId}", method = RequestMethod.GET)
    @GetMapping("/{bookId}")
    @ApiOperation("根据路径传参获取图书")
    @ApiImplicitParam(name = "bookId", value = "图书id", example = "0")
    public ServerResponse find(@PathVariable("bookId") Long id) {
        return bookService.findBook(id);
    }

//    @RequestMapping(method = RequestMethod.PUT)
    @PutMapping
    @ApiOperation("更新图书")
    public ServerResponse update(@RequestBody Book book) {
        return bookService.updateBook(book);
    }
}
